home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / libsrc.lzh / LibSrc / Math / frexp.c < prev    next >
C/C++ Source or Header  |  1990-04-07  |  2KB  |  63 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include "pml.h"
  4.  
  5. /*
  6.  * Frexp returns the significand of a double "value" as a double quanity,
  7.  * "x", of magnitude less than 1 and stores an integer "n", indirectly
  8.  * through "eptr", such that "value" = "x" * 2^"n"
  9.  * 
  10.  * For the mc68000 using IEEE format the double precision word format is:
  11.  * 
  12.  * WORD N   =>    SEEEEEEEEEEEMMMM 
  13.  * WORD N+1 =>    MMMMMMMMMMMMMMMM 
  14.  * WORD N+2 =>    MMMMMMMMMMMMMMMM 
  15.  * WORD N+3 =>    MMMMMMMMMMMMMMMM
  16.  * 
  17.  * Where:          S  =>   Sign bit 
  18.  *                 E  =>   Exponent 
  19.  *                 X  =>   Ignored (set to 0)
  20.  *                 M  =>   Mantissa bit
  21.  * 
  22.  * NOTE:  Beware of 0.0; on some machines which use excess 128 notation for the
  23.  * exponent, if the mantissa is zero the exponent is also.
  24.  * 
  25.  */
  26.  
  27. #define MANT_MASK 0x800FFFFF    /* Mantissa extraction mask     */
  28. #define ZPOS_MASK 0x3FF00000    /* Positive # mask for exp = 0  */
  29. #define ZNEG_MASK 0x3FF00000    /* Negative # mask for exp = 0  */
  30.  
  31. #define EXP_MASK 0x7FF00000 /* Mask for exponent            */
  32. #define EXP_SHIFTS 20       /* Shifts to get into LSB's     */
  33. #define EXP_BIAS 1022       /* Exponent bias                */
  34.  
  35.  
  36. union dtol {
  37.     double          dval;
  38.     int             ival[2];
  39. };
  40.  
  41. double
  42. frexp(value, eptr)
  43.     double          value;
  44.     int            *eptr;
  45. {
  46.     union dtol      number;
  47.     int            *iptr, cexp, exp;
  48.  
  49.     if (value == 0.0) {
  50.         *eptr = 0;
  51.         return (0.0);
  52.     }
  53.     else {
  54.         number.dval = value;
  55.         iptr = &number.ival[0];
  56.         *eptr = (((*iptr) & EXP_MASK) >> EXP_SHIFTS) - EXP_BIAS;
  57.         *iptr &= ~EXP_MASK;
  58.         exp = EXP_BIAS;
  59.         *iptr |= (exp << EXP_SHIFTS) & EXP_MASK;
  60.         return (number.dval);
  61.     }
  62. }
  63.